import { getToken,setToken,removeToken } from "@/utils/auth"
import {login,getProfile,getUserDetailById}  from '@/api/user'
export default{
  namespaced:true,
  state:{
    token: getToken() || '',
    userInfo:{}
  },
  mutations:{
    // 更新token
    updateToken(state,token){
      state.token = token
      setToken(token)
    },
    // 删除token
    removeToken(state,token){
      state.token = ''
      removeToken()
    },
    // 更新用户信息
    updateUser(state,info){
      state.userInfo = info
    },
    // 删除用户信息
    deleteUser(state){
      state.userInfo = {}
    }
  },
  actions:{
    async updateLogin(context,loginForm){
      const res = await login(loginForm)
      console.log(res.data);
      context.commit('updateToken',res.data)
    },
    async getUserInfo(context){
      const res = await getProfile()
      console.log(res.data);
      const info = await getUserDetailById(res.data.userId)
      console.log(info.data);
      context.commit('updateUser',{...res.data,...info.data})
    },
    logout(context){
      context.commit('removeToken')
      context.commit('deleteUser')
    }
    
  }
}